package com.tanhua.api.impl.db;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.api.db.UserApi;
import com.tanhua.domain.db.User;
import com.tanhua.mapper.db.UserMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@Service
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;

    //服务方法已经在注册中心了 后期调用
    @Override
    public Long save(User user) {
        userMapper.insert(user); //mybatisPlus底层使用了主键返回策略
        return user.getId();
    }

    @Override
    public User findPhone(String phone) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("phone",phone);
        User user = userMapper.selectOne(qw);
        return user;
    }

    @Override
    public int getCount(){
        QueryWrapper<User> qw = new QueryWrapper<>();
        Integer count = userMapper.selectCount(qw);
        return count;
    }

    @Override
    public void updatePhone(Long id, String phone) {
        User user = new User();
        user.setPhone(phone);
        user.setId(id);
        user.setUpdated(new Date());
        userMapper.updateById(user);
    }

    @Override
    public User findUser(Long userId) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("id",userId);
        User user = userMapper.selectOne(qw);
        return user;
    }
}
